#include "../main.h"

/* 
--------------------------------------------------------------------------------
	函数功能：2.4G WIFI设置
--------------------------------------------------------------------------------
*/
int wifi_set_ra0(int sockfd, char *tagid, char *msg)
{
	int ret = 0;
	char ssid[32];
	char key[64];
	char encry[12];
	char hidden[4];
	char data[1024];
	cJSON *pRow = NULL, *pRoot = NULL, *pValue = NULL;

	pRow = cJSON_CreateObject();

	pRoot = cJSON_Parse(msg);
	if (pRoot == NULL)
	{
		cJSON_AddNumberToObject(pRow, "code", 1);
		cJSON_AddStringToObject(pRow, "msg", "JSON解析失败!");
	}
	else
	{
		memset(ssid, 0, sizeof(ssid));
		pValue = cJSON_GetObjectItem(pRoot, "username");
		if (pValue != NULL && pValue->type == cJSON_String)
			memcpy(ssid, pValue->valuestring, sizeof(ssid));

		memset(key, 0, sizeof(key));
		pValue = cJSON_GetObjectItem(pRoot, "password");
		if (pValue != NULL && pValue->type == cJSON_String)
			memcpy(key, pValue->valuestring, sizeof(key));

		memset(encry, 0, sizeof(encry));
		pValue = cJSON_GetObjectItem(pRoot, "eryType");
		if (pValue != NULL && pValue->type == cJSON_String)
			memcpy(encry, pValue->valuestring, sizeof(encry));
		else
			memcpy(encry, "none", sizeof(encry));

		memset(hidden, 0, sizeof(hidden));
		pValue = cJSON_GetObjectItem(pRoot, "hide");
		if (pValue != NULL && (pValue->type == cJSON_False || pValue->type == cJSON_True || pValue->type == cJSON_Number))
		{
			if (pValue->valueint == 1)
				memcpy(hidden, "1", sizeof(hidden));
			else
				memcpy(hidden, "0", sizeof(hidden));
		}
		else
			memcpy(hidden, "0", sizeof(hidden));

		if (!strlen(ssid) || strlen(ssid) > 32 || (strcmp(encry, "none") && !strlen(key)))
		{
			cJSON_AddNumberToObject(pRow, "code", 1);
			cJSON_AddStringToObject(pRow, "msg", "2.4G WIFI设置,参数错误");
		}
		else
		{
			set_value("wireless.ra0.ssid", ssid);
			set_value("wireless.ra0.encryption", encry);
			set_value("wireless.ra0.hidden", hidden);

			if (strlen(key))
				set_value("wireless.ra0.key", key);

			//system("sleep 3 && wifi down && sleep 1 && wifi up &");
			system("sleep 3 && /etc/init.d/network restart &");

			cJSON_AddNumberToObject(pRow, "code", 0);
			cJSON_AddStringToObject(pRow, "msg", "Success");
		}
	}

	pRoot = cJSON_CreateObject();
	cJSON_AddStringToObject(pRoot, "type", "COMMON_SUCCESS");
	cJSON_AddStringToObject(pRoot, "replyType", "DEVICE_TWOWIFI_SET");
	cJSON_AddStringToObject(pRoot, "clientId", RouteID);
	cJSON_AddStringToObject(pRoot, "tagClientId", tagid);
	cJSON_AddStringToObject(pRoot, "content", cJSON_Print(pRow));

	char *buffer = cJSON_Print(pRoot);
	buffer[strlen(buffer)] = '\0';

	if (pRow != NULL)
		cJSON_Delete(pRow);

	if (pRoot != NULL)
		cJSON_Delete(pRoot);

	ret = send_msg(sockfd, buffer, strlen(buffer));
	free(buffer);
	buffer = NULL;

	return ret;
}

/* 
--------------------------------------------------------------------------------
	函数功能：2.4G WIFI获取
--------------------------------------------------------------------------------
*/
int wifi_get_ra0(int sockfd, char *tagid)
{
	int ret = 0;
	cJSON *pRoot = NULL, *pRow = NULL;
	pRow = cJSON_CreateObject();

	char *ssid = get_value_change("wireless.ra0.ssid");
	char *key = get_value_change("wireless.ra0.key");
	char *encryption = get_value_change("wireless.ra0.encryption");
	char *hidden = get_value_change("wireless.ra0.hidden");

	if (char_has_value(ssid) > 0)
	{
		cJSON_AddStringToObject(pRow, "username", ssid);
	}
	if (key != NULL)
	{
		cJSON_AddStringToObject(pRow, "password", key);
	}
	if (char_has_value(encryption) > 0)
	{
		cJSON_AddStringToObject(pRow, "eryType", encryption);
	}
	else
	{
		cJSON_AddStringToObject(pRow, "eryType", "none");
	}
	if (char_has_value(hidden) > 0)
	{
		cJSON_AddBoolToObject(pRow, "hide", atoi(hidden));
	}
	else
	{
		cJSON_AddBoolToObject(pRow, "hide", 0);
	}

	pRoot = cJSON_CreateObject();
	cJSON_AddStringToObject(pRoot, "type", "DEVICE_TWOWIFIGET_REP");
	cJSON_AddStringToObject(pRoot, "replyType", "DEVICE_TWOWIFIGET_REQ");
	cJSON_AddStringToObject(pRoot, "clientId", RouteID);
	cJSON_AddStringToObject(pRoot, "tagClientId", tagid);
	cJSON_AddStringToObject(pRoot, "content", cJSON_Print(pRow));

	char *buffer = cJSON_Print(pRoot);
	buffer[strlen(buffer)] = '\0';

	if (pRoot != NULL)
	{
		cJSON_Delete(pRoot);
	}

	ret = send_msg(sockfd, buffer, strlen(buffer));

	delete_mem(ssid);
	delete_mem(key);
	delete_mem(encryption);
	delete_mem(hidden);

	delete_mem(buffer);

	return ret;
}

/* 
--------------------------------------------------------------------------------
	函数功能：5.8G WIFI设置
--------------------------------------------------------------------------------
*/
int wifi_set_rai0(int sockfd, char *tagid, char *msg)
{
	int ret = 0;
	char ssid[32];
	char key[64];
	char encry[12];
	char hidden[4];
	char data[1024];
	cJSON *pRow = NULL, *pRoot = NULL, *pValue = NULL;

	pRow = cJSON_CreateObject();

	pRoot = cJSON_Parse(msg);
	if (pRoot == NULL)
	{
		cJSON_AddNumberToObject(pRow, "code", 1);
		cJSON_AddStringToObject(pRow, "msg", "JSON解析失败!");
	}
	else
	{
		memset(ssid, 0, sizeof(ssid));
		pValue = cJSON_GetObjectItem(pRoot, "username");
		if (pValue != NULL && pValue->type == cJSON_String)
			memcpy(ssid, pValue->valuestring, sizeof(ssid));

		memset(key, 0, sizeof(key));
		pValue = cJSON_GetObjectItem(pRoot, "password");
		if (pValue != NULL && pValue->type == cJSON_String)
			memcpy(key, pValue->valuestring, sizeof(key));

		memset(encry, 0, sizeof(encry));
		pValue = cJSON_GetObjectItem(pRoot, "eryType");
		if (pValue != NULL && pValue->type == cJSON_String)
			memcpy(encry, pValue->valuestring, sizeof(encry));
		else
			memcpy(encry, "none", sizeof(encry));

		memset(hidden, 0, sizeof(hidden));
		pValue = cJSON_GetObjectItem(pRoot, "hide");
		if (pValue != NULL && (pValue->type == cJSON_False || pValue->type == cJSON_True || pValue->type == cJSON_Number))
		{
			if (pValue->valueint == 1)
				memcpy(hidden, "1", sizeof(hidden));
			else
				memcpy(hidden, "0", sizeof(hidden));
		}
		else
			memcpy(hidden, "0", sizeof(hidden));

		if (!strlen(ssid) || strlen(ssid) > 32 || (strcmp(encry, "none") && !strlen(key)))
		{
			cJSON_AddNumberToObject(pRow, "code", 1);
			cJSON_AddStringToObject(pRow, "msg", "2.4G WIFI设置,参数错误");
		}
		else
		{
			set_value("wireless.rai0.ssid", ssid);
			set_value("wireless.rai0.encryption", encry);
			set_value("wireless.rai0.hidden", hidden);

			if (strlen(key))
				set_value("wireless.rai0.key", key);

			//system("sleep 3 && wifi down && sleep 1 && wifi up &");
			system("sleep 3 && /etc/init.d/network restart &");

			cJSON_AddNumberToObject(pRow, "code", 0);
			cJSON_AddStringToObject(pRow, "msg", "Success");
		}
	}

	pRoot = cJSON_CreateObject();
	cJSON_AddStringToObject(pRoot, "type", "COMMON_SUCCESS");
	cJSON_AddStringToObject(pRoot, "replyType", "DEVICE_FIVEWIFI_SET");
	cJSON_AddStringToObject(pRoot, "clientId", RouteID);
	cJSON_AddStringToObject(pRoot, "tagClientId", tagid);
	cJSON_AddStringToObject(pRoot, "content", cJSON_Print(pRow));

	char *buffer = cJSON_Print(pRoot);
	buffer[strlen(buffer)] = '\0';

	if (pRow != NULL)
		cJSON_Delete(pRow);

	if (pRoot != NULL)
		cJSON_Delete(pRoot);

	ret = send_msg(sockfd, buffer, strlen(buffer));
	free(buffer);
	buffer = NULL;

	return ret;
}

/* 
--------------------------------------------------------------------------------
	函数功能：5.8G WIFI获取
--------------------------------------------------------------------------------
*/
int wifi_get_rai0(int sockfd, char *tagid)
{
	int ret = 0;
	cJSON *pRoot = NULL, *pRow = NULL;
	pRow = cJSON_CreateObject();

	char *ssid = get_value_change("wireless.rai0.ssid");
	char *key = get_value_change("wireless.rai0.key");
	char *encryption = get_value_change("wireless.rai0.encryption");
	char *hidden = get_value_change("wireless.rai0.hidden");
	if (char_has_value(ssid) > 0)
	{
		cJSON_AddStringToObject(pRow, "username", ssid);
	}
	if (key != NULL)
	{
		cJSON_AddStringToObject(pRow, "password", key);
	}
	if (char_has_value(encryption) > 0)
	{
		cJSON_AddStringToObject(pRow, "eryType", encryption);
	}
	else
	{
		cJSON_AddStringToObject(pRow, "eryType", "none");
	}
	if (char_has_value(hidden) > 0)
	{
		cJSON_AddBoolToObject(pRow, "hide", atoi(hidden));
	}
	else
	{
		cJSON_AddBoolToObject(pRow, "hide", 0);
	}
	pRoot = cJSON_CreateObject();
	cJSON_AddStringToObject(pRoot, "type", "DEVICE_TWOWIFIGET_REP");
	cJSON_AddStringToObject(pRoot, "replyType", "DEVICE_TWOWIFIGET_REQ");
	cJSON_AddStringToObject(pRoot, "clientId", RouteID);
	cJSON_AddStringToObject(pRoot, "tagClientId", tagid);
	cJSON_AddStringToObject(pRoot, "content", cJSON_Print(pRow));

	char *buffer = cJSON_Print(pRoot);
	buffer[strlen(buffer)] = '\0';
	OUT_DEBUG(DEBUG, "3");
	if (pRoot != NULL)
	{
		cJSON_Delete(pRoot);
	}

	ret = send_msg(sockfd, buffer, strlen(buffer));
	OUT_DEBUG(DEBUG, "4");
	delete_mem(ssid);
	delete_mem(key);
	delete_mem(encryption);
	delete_mem(hidden);

	delete_mem(buffer);

	return ret;
}

/* 
--------------------------------------------------------------------------------
	函数功能：访客WIFI设置
--------------------------------------------------------------------------------
*/
int wifi_set_ra1(int sockfd, char *tagid, char *msg)
{
	int ret = 0;
	char ssid[32];
	char key[64];
	char encry[12];
	char hidden[4];
	char data[1024];
	cJSON *pRow = NULL, *pRoot = NULL, *pValue = NULL;

	pRow = cJSON_CreateObject();

	pRoot = cJSON_Parse(msg);
	if (pRoot == NULL)
	{
		cJSON_AddNumberToObject(pRow, "code", 1);
		cJSON_AddStringToObject(pRow, "msg", "JSON解析失败!");
	}
	else
	{
		memset(ssid, 0, sizeof(ssid));
		pValue = cJSON_GetObjectItem(pRoot, "username");
		if (pValue != NULL && pValue->type == cJSON_String)
			memcpy(ssid, pValue->valuestring, sizeof(ssid));

		memset(key, 0, sizeof(key));
		pValue = cJSON_GetObjectItem(pRoot, "password");
		if (pValue != NULL && pValue->type == cJSON_String)
			memcpy(key, pValue->valuestring, sizeof(key));

		memset(encry, 0, sizeof(encry));
		pValue = cJSON_GetObjectItem(pRoot, "eryType");
		if (pValue != NULL && pValue->type == cJSON_String)
			memcpy(encry, pValue->valuestring, sizeof(encry));
		else
			memcpy(encry, "none", sizeof(encry));

		memset(hidden, 0, sizeof(hidden));
		pValue = cJSON_GetObjectItem(pRoot, "hide");
		if (pValue != NULL && (pValue->type == cJSON_False || pValue->type == cJSON_True || pValue->type == cJSON_Number))
		{
			if (pValue->valueint == 1)
				memcpy(hidden, "1", sizeof(hidden));
			else
				memcpy(hidden, "0", sizeof(hidden));
		}
		else
			memcpy(hidden, "0", sizeof(hidden));

		if (!strlen(ssid) || strlen(ssid) > 32 || (strcmp(encry, "none") && !strlen(key)))
		{
			cJSON_AddNumberToObject(pRow, "code", 1);
			cJSON_AddStringToObject(pRow, "msg", "2.4G WIFI设置,参数错误");
		}
		else
		{
			set_value("wireless.ra1.ssid", ssid);
			set_value("wireless.ra1.encryption", encry);
			set_value("wireless.ra1.hidden", hidden);

			if (strlen(key))
				set_value("wireless.ra1.key", key);

			//system("sleep 3 && wifi down && sleep 1 && wifi up &");
			system("sleep 3 && /etc/init.d/network restart &");

			cJSON_AddNumberToObject(pRow, "code", 0);
			cJSON_AddStringToObject(pRow, "msg", "Success");
		}
	}

	pRoot = cJSON_CreateObject();
	cJSON_AddStringToObject(pRoot, "type", "COMMON_SUCCESS");
	cJSON_AddStringToObject(pRoot, "replyType", "DEVICE_CUSTOMERWIFI_SET");
	cJSON_AddStringToObject(pRoot, "clientId", RouteID);
	cJSON_AddStringToObject(pRoot, "tagClientId", tagid);
	cJSON_AddStringToObject(pRoot, "content", cJSON_Print(pRow));

	char *buffer = cJSON_Print(pRoot);
	buffer[strlen(buffer)] = '\0';

	if (pRow != NULL)
		cJSON_Delete(pRow);

	if (pRoot != NULL)
		cJSON_Delete(pRoot);

	ret = send_msg(sockfd, buffer, strlen(buffer));
	free(buffer);
	buffer = NULL;

	return ret;
}

/* 
--------------------------------------------------------------------------------
	函数功能：访客WIFI获取
--------------------------------------------------------------------------------
*/
int wifi_get_ra1(int sockfd, char *tagid)
{
	int ret = 0;
	cJSON *pRoot = NULL, *pRow = NULL;
	pRow = cJSON_CreateObject();

	char *ssid = get_value_change("wireless.ra1.ssid");
	char *key = get_value_change("wireless.ra1.key");
	char *encryption = get_value_change("wireless.ra1.encryption");
	char *hidden = get_value_change("wireless.ra1.hidden");

	if (char_has_value(ssid) > 0)
	{
		cJSON_AddStringToObject(pRow, "username", ssid);
	}
	if (key != NULL)
	{
		cJSON_AddStringToObject(pRow, "password", key);
	}
	if (char_has_value(encryption) > 0)
	{
		cJSON_AddStringToObject(pRow, "eryType", encryption);
	}
	else
	{
		cJSON_AddStringToObject(pRow, "eryType", "none");
	}
	if (char_has_value(hidden) > 0)
	{
		cJSON_AddBoolToObject(pRow, "hide", atoi(hidden));
	}
	else
	{
		cJSON_AddBoolToObject(pRow, "hide", 0);
	}

	pRoot = cJSON_CreateObject();
	cJSON_AddStringToObject(pRoot, "type", "DEVICE_TWOWIFIGET_REP");
	cJSON_AddStringToObject(pRoot, "replyType", "DEVICE_TWOWIFIGET_REQ");
	cJSON_AddStringToObject(pRoot, "clientId", RouteID);
	cJSON_AddStringToObject(pRoot, "tagClientId", tagid);
	cJSON_AddStringToObject(pRoot, "content", cJSON_Print(pRow));

	char *buffer = cJSON_Print(pRoot);
	buffer[strlen(buffer)] = '\0';

	if (pRoot != NULL)
	{
		cJSON_Delete(pRoot);
	}

	ret = send_msg(sockfd, buffer, strlen(buffer));

	delete_mem(ssid);
	delete_mem(key);
	delete_mem(encryption);
	delete_mem(hidden);

	delete_mem(buffer);

	return ret;
}
